﻿# -*- coding: utf-8 -*-
import os,sys
import subprocess

# Imports the monkeyrunner modules used by this program
class ScrollActions:
    FastUpScroll, FastDownScroll, SlowUpScroll, SlowDownScroll = ("fu","fd","su","sd")

def scrollTest(action, count):
    #向下滑动屏幕;
    if action == ScrollActions.FastUpScroll:
        for i in range(count):
            device.drag((device_half_width,scroll_min_height),(device_half_width,scroll_max_height),0.5,10)#开始，结束，持续时间，步骤
    elif action == ScrollActions.FastDownScroll:
        for i in range(count):
            device.drag((device_half_width,scroll_max_height),(device_half_width,scroll_min_height),0.5,10)#开始，结束，持续时间，步骤 
    elif action == ScrollActions.SlowDownScroll:
        for i in range(count):
            device.drag((device_half_width,scroll_max_height),(device_half_width,scroll_min_height),1,10)#开始，结束，持续时间，步骤
    elif action == ScrollActions.SlowUpScroll:
        for i in range(count):
            device.drag((device_half_width,scroll_min_height),(device_half_width,scroll_max_height),1,10)#开始，结束，持续时间，步骤
    
if __name__ == "__main__":
    from optparse import OptionParser
    usage = """
	usage:python scrolltest.py [options]
    eg:
    control the listview scroll up fast
    python scrolltest.py -a fu
    control the listview fast scroll up && slow scroll down
    python scrolltest.py -a fu -a sd
    """
    parser = OptionParser(usage)
    parser.add_option('-a', '--actions', dest='actions', help="can set mutiple actions,FastUpScroll(fu),FastDownScroll(fd),SlowUpScroll(su),SlowDownScroll(sd)", metavar="ScrollActions", action="append",default=[])
    parser.add_option('-n', '--count', dest='count', help="set each action running times,default value is 20", metavar="ScrollActions", default=20)
    parser.add_option('--activity', dest='activity', help="the name of the activity, for example: -v com.tencent.mobileqq/com.tencent.mobileqq.activity.ChatActivity")
    parser.add_option('-o', '--outfile', dest='outfile', default='fps_result.csv', help='The result output file')
    (options, args) = parser.parse_args()

    if (not options.actions):
        print "please set a action eg:python scrolltest.py -a fu"
        parser.print_help()
        exit(0)
    if (not options.activity):
        print "please set a activity name: python scrolltest.py -a fu --activity com.tencent.mobileqq/com.tencent.mobileqq.activity.ChatActivity"
        parser.print_help()
        exit(0)
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
    # Connects to the current device, returning a MonkeyDevice object
    device = MonkeyRunner.waitForConnection()
    device_width = int(device.getProperty('display.width'))
    device_height = int(device.getProperty('display.height'))
    scroll_min_height = int(device_height * 0.2)
    scroll_max_height = int(device_height * 0.8)
    device_half_width = int(device_width / 2)
    
    # Installs the Android package. Notice that this method returns a boolean, so you can test
    MonkeyRunner.sleep(3)
    # Runs the component
    #device.startActivity(component='com.tencent.optimus/com.tencent.optimus.MainActivity')
    if not device:
        print("Please connect a device to start!")
        sys.exit()
    else:
        print("Start...")
    subprocess.Popen("start cmd.exe /C "+"python fpsmonitor.py" + " -a " + options.activity + " -o " + options.outfile, shell=True)
    MonkeyRunner.sleep(2)
    
    for action in options.actions:
        scrollTest(action, options.count)
